﻿using AppFramework.Logger;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using static AppMachine.Models.LogListModel.Types;

namespace AppMachine.Models {

    public class LogListModel : Prism.Mvvm.BindableBase {

        public ObservableCollection<Types.LogModel> LogList { get; } = [];
        public LogListModel() {
            ILogger.Default.OnLogYield += Default_OnLogYield;
        }

        private int mfMaxLogCount = 500;
        private void Default_OnLogYield(LogInfo logInfo) {
            App.Current.Dispatcher.BeginInvoke(() => {
                if (logInfo != null) {
                    this.LogList.Insert(0, new LogModel(logInfo));
                    if (this.LogList.Count > this.mfMaxLogCount) {
                        this.LogList.RemoveAt(this.mfMaxLogCount);
                    }
                }
            });
        }

        public static class Types {
            public class LogModel(LogInfo logInfo) : Prism.Mvvm.BindableBase {
                private readonly LogInfo mfLogInfo = logInfo;

                public string OccurTime => $"{this.mfLogInfo.DateTime:yyyy-MM-dd HH:mm:ss.fff}";
                public string LogText=>this.mfLogInfo.LogText;
            }
        }

    }

}
